package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.StationDto;
import com.huike.entity.*;
import com.huike.exception.BaseException;
import com.huike.exception.DeletionNotAllowedException;
import com.huike.mapper.RoleMapper;
import com.huike.mapper.StationMapper;
import com.huike.mapper.SysDictDataMapper;
import com.huike.mapper.SysUserMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.StationsService;
import com.huike.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static jdk.nashorn.internal.objects.Global.print;

@Slf4j
@Service
@Transactional
public class StationsServiceImpl extends ServiceImpl<StationMapper, Station> implements StationsService {
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageResult findAll(StationDto dto) {
        IPage page = new Page(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<Station> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (dto.getPostCode() != null) {
            lambdaQueryWrapper.like(Station::getPostCode, dto.getPostCode());
        }
        if (dto.getPostName() != null) {
            lambdaQueryWrapper.like(Station::getPostName, dto.getPostName());
        }
        if (dto.getStatus() != null) {
            lambdaQueryWrapper.eq(Station::getStatus, dto.getStatus());
        }
        /*page = page(page, lambdaQueryWrapper);*/
        lambdaQueryWrapper.orderByAsc(Station::getPostSort);
        if (page.getSize()>10){
            page.setSize(10);
        }
        stationMapper.selectPage(page, lambdaQueryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());

        return pageResult;
    }

    @Override
    public Result dictlabr(String dictLabel) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictData::getDictType, dictLabel);
        List<SysDictData> sysDictData = sysDictDataMapper.selectList(lambdaQueryWrapper);

        return Result.success(sysDictData);
    }


    @Override
    public void add(StationDto dto) {
   /*     if (dto.getPostName() == null || dto.getPostName().length() > 12) {
            throw new BaseException("岗位输入有误");
        }*/

        List<Station> stations = stationMapper.selectList(null);
        for (Station station : stations) {
            String postName = station.getPostName();
            if (postName!=null){
                if (postName.equals(dto.getPostName())) {
                    throw new BaseException("岗位名字重复");
                }
            }

        }
    /*    if (dto.getStatus() == null) {
            throw new BaseException("状态不能为空");
        }*/
        /*if (dto.getPostCode()==null){
            throw new BaseException("岗位编码不能为空");
        }
        if (dto.getPostSort()==null){
            throw new BaseException("显示顺序不能为空");
        }*/

       /* Long currentId = BaseContext.getCurrentId();
        SysUser sysUser = sysUserMapper.selectById(currentId);
        String userName = sysUser.getUserName();*/
        Station station = new Station();
        BeanUtils.copyProperties(dto, station);
        station.setCreateTime(new Date());
        station.setCreateBy("admin");
        station.setPostCode("a");
        station.setPostSort(0);
       /* station.setCreateBy(userName);
        station.setUpdateBy(userName);*/
        stationMapper.insert(station);

    }

    @Override
    public void deleteById(Integer ids) {
       /* for (Integer id : ids) {
            Station station = stationMapper.selectById(id);
            if ("0".equals(station.getStatus())){
                throw new BaseException(station.getPostName()+"岗位正在使用,不能删除");
            }

*/
      Station station = stationMapper.selectById(ids);
        if ("0".equals(station.getStatus())) {
            throw new BaseException(station.getPostName()+"岗位正在使用,不能删除");
        }
            stationMapper.deleteById(ids);

    }

    @Override
    public void updateStation(StationDto dto) {
        Station station = new Station();
        BeanUtils.copyProperties(station, dto);
        station.setCreateTime(new Date());
        if ("1".equals(dto.getStatus())){
            station.setStatus(String.valueOf(1));
        }
        stationMapper.updateById(station);
    }

    @Override
    public Result findById(Integer id) {
        Station station = stationMapper.selectById(id);
        return Result.success(station);
    }
}
